package gmmxEditor;

import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;

import mh.entity.Entity;
import mh.entity.EnumState;
import mh.map.render.Render;

public class SimpleRender extends Render implements ISimpleDrawable {

	public SimpleRender(Render r) {
		super(r, null);
	}

	/**
	 * Draws the image using only the standard AWT.
	 * 
	 * @param g
	 *            Graphics where to draw the image
	 * @param e
	 *            Entity with related params
	 * @param posX
	 *            The position where to draw the image
	 * @param posY
	 *            The position where to draw the image
	 */
	@Override
	public void draw(Graphics g, Entity e, int posX, int posY) {
		if (this.image == null) {
			return;
		}
		Rectangle state = null;
		if (e.getStates().size() > 0) {
			EnumState s = e.getDamageState();
			state = this.statesPosition.get(s);
		} else {
			state = this.statesPosition.get(EnumState.INTACT);
		}
		if (state == null) {
			state = this.statesPosition.get(EnumState.INTACT);
		}

		state.width = (int) (state.getWidth() / this.getStateFrames(EnumState.INTACT));

		g.drawImage(this.image, posX, posY, posX + (int) state.getWidth(), posY + (int) state.getHeight(), (int) state.getX(), (int) state.getY(),
				(int) state.getX() + (int) state.getWidth(), (int) state.getY() + (int) state.getHeight(), null);
	}

	@Override
	public void draw(Graphics g, EnumState s, int posX, int posY) {
		if (this.image == null) {
			return;
		}
		Rectangle state = this.statesPosition.get(s);
		if (state == null) {
			state = this.statesPosition.get(EnumState.INTACT);
			s = EnumState.INTACT;
		}

		int width = (int) (state.getWidth() / this.getStateFrames(s));

		if (this.isMask(s)) {//if the state is a mask should be first drawn the intact state
			EnumState baseState = EnumState.INTACT;
			Rectangle baseStateRect = this.statesPosition.get(baseState);
			g.drawImage(this.image, posX, posY, posX + (int) baseStateRect.getWidth(), posY + (int) baseStateRect.getHeight(), (int) baseStateRect.getX(),
					(int) baseStateRect.getY(), (int) baseStateRect.getX() + (int) baseStateRect.getWidth(),
					(int) baseStateRect.getY() + (int) baseStateRect.getHeight(), null);
		}

		g.drawImage(this.image, posX, posY, posX + width, posY + (int) state.getHeight(), (int) state.getX(), (int) state.getY(), (int) state.getX() + width,
				(int) state.getY() + (int) state.getHeight(), null);

	}

	/**
	 * Nothing to be done here because simple renderer is not drawable with OpenGL
	 */
	@Override
	public void draw(long delta, double posX, double posY, boolean foreground) {
		return;
	}

	@Override
	public BufferedImage getImage() {
		return this.image;
	}

	/**
	 * Returns the number of frames of the specified state
	 * 
	 * @param state
	 * @return int
	 */
	public int getStateFrames(EnumState state) {
		return this.statesRectangleAnimationData.get(super.statesPosition.get(state)).getLeft();
	}

	@Override
	public Size getSize() {
		return new Size((int) super.statesPosition.get(EnumState.INTACT).getWidth() / this.getStateFrames(EnumState.INTACT), (int) super.statesPosition.get(
				EnumState.INTACT).getHeight());
	}

	@Override
	public String toString() {
		return this.image + " . " + this.statesPosition;
	}
}
